//
//  EWMViewController.swift
//  ExList
//
//  Created by qmsoft_macmini on 16/6/27.
//  Copyright © 2016年 qmsoft_macmini. All rights reserved.
//

import UIKit

class EWMViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var txt: UILabel!
    @IBOutlet weak var tf: UITextField!

    @IBOutlet weak var item_sm: UIBarButtonItem!
    @IBOutlet weak var iv_ewm: UIImageView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tf.delegate = self
    }
    //当用户按下return键或者按回车键，keyboard消失
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func onSM(sender: UIBarButtonItem) {
        
    }

    @IBAction func onSC(sender: UIBarButtonItem) {
//       let ewm = EWMCode.getQRCodeImage(tf.text!, sizeWH: 200)
//        iv_ewm.image = ewm
        iv_ewm.image = createQRForString(tf.text, qrImageName: "cloud")
    }
    
    @IBAction func onSB(sender: UIButton) {
        //代码截屏
        //(1)设置要截屏的图片的大小
        UIGraphicsBeginImageContext(self.view.frame.size);
        //(2)对哪个视图截图固定大小的图片
        self.view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
        //(3)获取截图的图片对象
        let image = UIGraphicsGetImageFromCurrentImageContext();
        //(4)结束绘制图片
        UIGraphicsEndImageContext();
        //(5)保存到相册
        UIImageWriteToSavedPhotosAlbum(image, nil,nil, nil);
        let ss = EWMCode.redImage(image)
        print("======\(ss)")
        let ewm = EWMCode()
        ewm.readQrCodeFromePhotoLibrary(self) { (qrMessage) in
            print("结果\(qrMessage)")
        }
    }
    private func createQRForString(qrString: String?, qrImageName: String?) -> UIImage?{
        if let sureQRString = qrString {
            let stringData = sureQRString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
            // 创建一个二维码的滤镜
            if let qrFilter = CIFilter(name: "CIQRCodeGenerator") {
                qrFilter.setValue(stringData, forKey: "inputMessage")
                qrFilter.setValue("H", forKey: "inputCorrectionLevel")
                let qrCIImage = qrFilter.outputImage
                // 创建一个颜色滤镜,黑白色
                if let colorFilter = CIFilter(name: "CIFalseColor") {
                    colorFilter.setDefaults()
                    colorFilter.setValue(qrCIImage, forKey: "inputImage")
                    colorFilter.setValue(CIColor(red: 0, green: 0, blue: 0), forKey: "inputColor0")
                    colorFilter.setValue(CIColor(red: 1, green: 1, blue: 1), forKey: "inputColor1")
                    if let img = colorFilter.outputImage?.imageByApplyingTransform(CGAffineTransformMakeScale(5, 5)) {
                        // 返回二维码image
                        let codeImage = UIImage(CIImage: img)
                        // 通常,二维码都是定制的,中间都会放想要表达意思的图片
                        if let name = qrImageName {
                            if let iconImage = UIImage(named: name) {
                                let rect = CGRectMake(0, 0, codeImage.size.width, codeImage.size.height)
                                UIGraphicsBeginImageContext(rect.size)
                                
                                codeImage.drawInRect(rect)
                                let avatarSize = CGSizeMake(rect.size.width * 0.25, rect.size.height * 0.25)
                                let x = (rect.width - avatarSize.width) * 0.5
                                let y = (rect.height - avatarSize.height) * 0.5
                                iconImage.drawInRect(CGRectMake(x, y, avatarSize.width, avatarSize.height))
                                let resultImage = UIGraphicsGetImageFromCurrentImageContext()
                                
                                UIGraphicsEndImageContext()
                                return resultImage
                            }
                        }
                        return codeImage
                    }
                }
            }
        }
        return nil
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
